package com.lmb.controller;

import com.lmb.bean.AdministratorLogin;
import com.lmb.bean.Reset;
import com.lmb.bean.UserLogin;
import com.lmb.mapper.AdministratorLoginDao;
import com.lmb.service.AdministratorLoginService;
import com.lmb.util.Page;
import com.lmb.util.PasswordUtil;
import com.lmb.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin //允许跨域访问
@RequestMapping("/adlogin")
public class AdministratorLoginController {
    @Autowired
    AdministratorLoginService AdministratorLoginService;
    @Autowired
    private AdministratorLoginDao AdministratorLoginDao;
    @PostMapping
    public Result add(@RequestBody AdministratorLogin AdministratorLogin){
        String pwd= AdministratorLogin.getAd_password();
        AdministratorLogin.setAd_password(PasswordUtil.getEncryption(pwd));
        Object obj= AdministratorLoginService.add(AdministratorLogin);
        return Result.success("数据添加成功",obj);
    }
    @DeleteMapping("{id}")
    public Result del(@PathVariable("id") Integer id){
        Object obj= AdministratorLoginService.del(id);
        return Result.success("数据删除成功",obj);
    }
    @PutMapping
    public Result edit(@RequestBody AdministratorLogin AdministratorLogin){
        String pwd= AdministratorLogin.getAd_password();
        AdministratorLogin.setAd_password(PasswordUtil.getEncryption(pwd));
        Object obj= AdministratorLoginService.edit(AdministratorLogin);
        return Result.success("修改保存成功",obj);
    }
    @GetMapping("all")
    public Result all( AdministratorLogin AdministratorLogin){
        Object obj= AdministratorLoginService.all(AdministratorLogin);
        return Result.success(obj);
    }
    @GetMapping("{id}")
    public Result one(@PathVariable("id") Integer id){
        Object obj= AdministratorLoginService.one(id);
        return Result.success(obj);
    }
    @GetMapping("page")
    public Result page(Page page, AdministratorLogin AdministratorLogin){
        AdministratorLoginService.page(page, AdministratorLogin);
        return Result.success(page);
    }
    @PostMapping("reset")
    public Result reset(@RequestBody Reset reset){
        String pwd= reset.getOld_password();
        String new_pwd= reset.getNew_password();
        Integer ad_id=reset.getUser_id();
        try {
            AdministratorLogin user= AdministratorLoginService.reset(pwd,new_pwd,ad_id);
            return Result.success("密码修改成功",user);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail(500,e.getMessage());
        }

    }
}
